﻿#include "ssSnowflake.h"

namespace StarSeeker
{

ssSnowflake::ssSnowflake()
{
	m_timeStamp = 0;
	m_inc = 0;
	m_serverID = 0;
	m_timeStampBegin = SS_SNOW_BEGIN_TIMESTAMP;
}

ssSnowflake::~ssSnowflake()
{
}

void ssSnowflake::Init(unsigned short serverID, int timeStampBegin)
{
	m_serverID = serverID;
	m_timeStamp = timeStampBegin;
}

long long ssSnowflake::GetID()
{
	uuid_u	id;
	time_t st = ::time(0) - m_timeStampBegin;
	if (st != m_timeStamp)
	{
		m_inc = 0;
		m_timeStamp = st;
	}
	id.id32[0] = (int)m_timeStamp;
	id.inc[2] = m_serverID;
	id.inc[3] = m_inc++;
	return id.id;
}

ssSafeSnowflake::ssSafeSnowflake()
{
}

ssSafeSnowflake::~ssSafeSnowflake()
{
}

long long ssSafeSnowflake::GetSafeID()
{
	long long ret = 0;
	m_lock.Lock();
	ret = GetID();
	m_lock.Unlock();
	return ret;
}

}
